// 在页面或组件中导入
import validate from 'wechat-validate'
let timer = null
Page({
  // // 通过 behaviors 注入 validate 方法
  behaviors: [validate],
  data: {
    history: [],
    time: 0,
    isShow: false,
    account: 'xbsj001',
    password: '123456'
  },
  rules:{
    account: [
      { required: true, message: '请填写账号!' },
      { min: 2, max: 18, message: '请检查账号是否正确!' },
    ],
    password:[  
      { required: true, message: '请输入密码!' },
      { pattern: /^\d{6}$/, message: '请检查密码是否正确!' }
    ]
  },   
  //  登录  
  async hLogin() {
    const isAllValid = this.validate()
    console.log(isAllValid);
    if(!isAllValid) return
    const { account, password } = this.data
    const res = await wx.$http.post('/driver/login/account', { account, password })
    console.log(res);
    // 调用全局app
    const app = getApp()
    // 存储 token
    app.setToken('token', res.data)
    // 跳转页面
    const url = '/pages/user/user'
    wx.reLaunch({
      url
    })
  },
  getSMSCode() {
    console.log('点击了')
    let time = 5 // 初始数据
    // 倒计时
    timer = setInterval(() => {
      console.log(time)
      // 停止定时器
      if(--time < 0) return clearInterval(timer)
      this.setData({time}) // 渲染结果
    }, 1000)
  },
  hAccount() {
    this.setData({
      isShow: false
    })
  },
  hPhone() {
    this.setData({
      isShow: true
    })
  },
  onHide(){
    clearInterval(timer)
    this.setData({ time: 0 })
    console.log('生命周期页面， 处于不可见状态')  
  }
})